package com.starbaba.stat.service.impl;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.base.common.database.BaseDao;
import com.base.common.exception.AppException;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.model.dictionary.Constant;
import com.starbaba.stat.service.StatUserBackService;
import com.tuanzi.server.loan_base.model.db.stat.Stat_user_sms_back;
import com.tuanzi.server.loan_base.util.DateUtils;

@SuppressWarnings("rawtypes")
public class StatUserBackServiceImpl implements StatUserBackService {
	
	BaseDao dao=new BaseDao(Constant.LOAN_STAT_DB_POOL);

	@Override
	public List<Stat_user_sms_back> getInfoList(Map<String, Object> paramsMap, int start, int length,
			PageResponse pageResponse) throws AppException {
		
		List<Stat_user_sms_back> list=new ArrayList<Stat_user_sms_back>();
		
		String endtime = (String) paramsMap.get("endTime");
		
		if(StringUtils.isBlank(endtime)){ //查询全部，当天数据要包含
			
			list = getAllInfo(paramsMap, start, length, pageResponse);
			
		}else{
			
			String currentDate = DateUtils.getCurrentDate();
			Date cpeDate = Date.valueOf(endtime);
			Date cpcDate = Date.valueOf(currentDate);
			
			if(cpeDate.getTime()>=cpcDate.getTime()){ //查询全部，当天数据要包含
				
				list = getAllInfo(paramsMap, start, length, pageResponse);
				
			}else{ //不查当天数据
				
				list=getInfoNotIncludeToday(paramsMap, start, length, pageResponse);
			}
			
		}
		
		return list;
	}
	
	//查询所有信息，包括当前数值，当天数值为实时显示
	public List<Stat_user_sms_back> getAllInfo(Map<String, Object> paramsMap, int start, int length,
			PageResponse pageResponse) throws AppException{
		
		List<Stat_user_sms_back> list=new ArrayList<Stat_user_sms_back>();
		
		String cdate = DateUtils.getCurrentDate();
		
		StringBuilder bef_sql=new StringBuilder();
		bef_sql.append("select cdate, today_noactivate_count,first_sms_count,first_user_activate_count,three_sms_count,three_user_activate_count from stat_user_sms_back where 1=1");
		addCondition(bef_sql, paramsMap);
		bef_sql.append(" order by cdate desc");
		if(bef_sql.length()>0&&start>=0&&length>0){
			bef_sql.append(" limit "+start+" ,"+(length-1));
		}
		
		StringBuilder aft_sql=new StringBuilder();
		aft_sql.append("select count(1) from ");
		aft_sql.append("(SELECT DISTINCT phone,activity_type");
		aft_sql.append(" FROM loan.common_activity_register_record");
		aft_sql.append(" WHERE cdate = ? AND phone IN (SELECT mobnum FROM loan.comm_user WHERE phoneid IS NULL)) a");
		
		List<Stat_user_sms_back> infoList = dao.list(bef_sql.toString(), Stat_user_sms_back.class);
		
		int activateNum = dao.getInt(aft_sql.toString(), cdate);
		
		int count = dao.getInt("select count(1) from stat_user_sms_back");
		
		Stat_user_sms_back bean = new Stat_user_sms_back();
		bean.setToday_noactivate_count(String.valueOf(activateNum));
		bean.setCdate(cdate);
		
		list.add(bean);
		
		for (Stat_user_sms_back user : infoList) {
			list.add(user);
		}
		
		pageResponse.setITotalRecords(count+1);
		pageResponse.setITotalDisplayRecords(count+1);
			
		return list;
	}
	
	
	//查询不包含当天的信息
	public List<Stat_user_sms_back> getInfoNotIncludeToday(Map<String, Object> paramsMap, int start, int length,
			PageResponse pageResponse) throws AppException{
		
		StringBuilder bef_sql=new StringBuilder();
		bef_sql.append("select cdate, today_noactivate_count,first_sms_count,first_user_activate_count,three_sms_count,three_user_activate_count from stat_user_sms_back where 1=1");
		addCondition(bef_sql, paramsMap);
		bef_sql.append(" order by cdate desc");
		
		int count = dao.getInt("select count(1) from ("+bef_sql.toString()+") a");
		
		if(bef_sql.length()>0&&start>=0&&length>0){
			bef_sql.append(" limit "+start+" ,"+(length-1));
		}
		
		List<Stat_user_sms_back> list = dao.list(bef_sql.toString(), Stat_user_sms_back.class);
		
		pageResponse.setITotalRecords(count);
		pageResponse.setITotalDisplayRecords(count);
		
		return list;
	}
	
	
	//增加条件
	public void addCondition(StringBuilder sql,Map<String,Object> paramsMap){
		String startDate = (String) paramsMap.get("startTime");
		String endDate = (String) paramsMap.get("endTime");
		
		if(!StringUtils.isBlank(startDate)){
			sql.append(" and cdate>='"+startDate+"'");
		}
		
		if(!StringUtils.isBlank(endDate)){
			sql.append(" and cdate<='"+endDate+"'");
		}
	}

}
